/*
 * Copyright © 2010 SenseForce.com.
 * The Software shall be used for Good, not Evil.
 */

package com.senseforce.classes;

import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;

/**
 *
 * @author jcccn
 * @site http://www.senseforce.com/
 */
public class Sender extends Thread {
    private OutputStream os;
    private String message;
    public Sender(OutputStream os) {
        this.os = os;
        start();
    }

    public synchronized void send(String msg) {
        message = msg;
        notify();
    }

    public synchronized void run() {
        while(true) {
            if (message == null) {
                try{
                    wait();
                } catch(Exception e) {
                    
                }
            }
            if (message == null) {
                break;
            }
            try {
                os.write(message.getBytes());
                os.write("\r\n".getBytes());
            } catch (IOException e) {
                
            }
            message = null;
        }
    }

    public synchronized void stop(){
        message = null;
        notify();
    }
}
